import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Arrays;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class TestApp {
	public static void main(String[] args) {

		FootprintUtil util = new FootprintUtil();

		util.initQuestions();

		util.initCategories();

		Question[] questions = util.mQuestions;

		DocumentBuilderFactory docFactory = DocumentBuilderFactory
				.newInstance();

		try {
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.parse("C:\\questions.xml");

			FileInputStream ais = new FileInputStream("C:\\questions_en.txt");
			InputStreamReader isr = new InputStreamReader(ais);
			BufferedReader reader = new BufferedReader(isr);

			String line = reader.readLine();
			int lineIndex = 0;
			int questionIndex = 0;
			int emptyLines = 0;
			while (line != null) {
				if (line.trim().equalsIgnoreCase("")) {
					emptyLines++;
				} else {					
					if (emptyLines == 1) {
						questionIndex++;
						lineIndex = 0;
					}
					emptyLines = 0;
				}

				if (lineIndex == 0)
					questions[questionIndex].mQuestion = line;
				if (lineIndex == 1)
					questions[questionIndex].mMinText = line;
				if (lineIndex == 2)
					questions[questionIndex].mMaxText = line;
				if (lineIndex > 2
						&& lineIndex - 3 < questions[questionIndex].mAnswers.length)
					questions[questionIndex].mAnswers[lineIndex - 3].mAnswer = line;

				lineIndex++;
				line = reader.readLine();
			}

			NodeList qNodeList = doc
					.getElementsByTagName("question_answer_set");

			for (int i = 0; i < qNodeList.getLength(); i++) {
				int ai = 0;
				Element q = (Element) qNodeList.item(i);

				q.getAttributes().removeNamedItem("note");
				q.getAttributes().getNamedItem("left_text")
				.setTextContent(questions[i].mMinText);
				q.getAttributes().getNamedItem("q_str")
				.setTextContent(questions[i].mQuestion);
				q.getAttributes().getNamedItem("right_text")
				.setTextContent(questions[i].mMaxText);
				NodeList icons = q.getElementsByTagName("iconDisplay");
				while (icons.getLength() > 0) {
					q.removeChild(icons.item(0));
				}

				NodeList answers = q.getElementsByTagName("answer");
				for (int j = 0; j < answers.getLength(); j++) {
					Node node = answers.item(j);
					node.getAttributes().getNamedItem("ans_str")
							.setTextContent(questions[i].mAnswers[ai].mAnswer);
					ai++;
				}			
			}
			Transformer transformer = TransformerFactory.newInstance()
					.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");

			FileOutputStream fos = new FileOutputStream("C:\\questions_en1.xml");
			OutputStreamWriter writer = new OutputStreamWriter(fos);
			
			StreamResult result = new StreamResult(writer);
			DOMSource source = new DOMSource(doc);
			transformer.transform(source, result);
		} catch (SAXException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//
		// try {
		// FileOutputStream fos = new FileOutputStream("C:\\questions_en1.txt");
		// OutputStreamWriter writer = new OutputStreamWriter(fos);
		//
		// for (Question question : questions) {
		// writer.append("\n");
		// writer.append(question.mQuestion + "\n");
		// writer.append(question.mMinText + "\n");
		// writer.append(question.mMaxText + "\n");
		// Answer[] answers = question.mAnswers;
		// for (Answer answer : answers) {
		// writer.append(answer.mAnswer + "\n");
		// }
		// }
		//
		// writer.close();
		// } catch (FileNotFoundException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

		// for (Question q : questions) {
		// System.out.println(q.mQuestion);
		// }
		//
		// Category[] categories = util.mCategories;
		// for (Category c : categories) {
		// System.out.println(c.mCategoryID);
		// }

		// int[] answers = new int[questions.length];
		// Arrays.fill(answers, -1);
		// answers[0] = 4;
		// answers[4] = 4;
		// answers[9] = 3;
		// answers[18] = 2;
		// answers[19] = 0;
		// answers[20] = 0;
		// answers[21] = 5;
		// answers[22] = 0;
		// answers[23] = 2;
		// answers[24] = 2;
		// answers[25] = 2;
		// answers[32] = 0;
		// answers[33] = 5;
		// answers[34] = 5;
		// answers[35] = 5;
		// answers[36] = 5;
		// answers[37] = 1;
		// answers[38] = 4;
		// answers[41] = 5;

		// answers[0] = 0;
		// answers[4] = 0;
		// answers[9] = 0;
		// answers[18] = 0;
		// answers[19] = 4;
		// answers[20] = 1;
		// answers[21] = 0;
		// answers[22] = 6;
		// answers[23] = 0;
		// answers[24] = 0;
		// answers[25] = 0;
		// answers[32] = 3;
		// answers[33] = 0;
		// answers[34] = 0;
		// answers[35] = 0;
		// answers[36] = 0;
		// answers[37] = 4;
		// answers[38] = 0;
		// answers[41] = 0;
		//		
		// FootprintResult result = util.calculateFootprintFromAnswers(answers);
		//
		// System.out.println(result.numberOfEarths);
		// System.out.println(result.builtTotal);
		// System.out.println(result.cropTotal);
		// System.out.println(result.energyTotal);
		// System.out.println(result.fishTotal);
		// System.out.println(result.foodTotal);
		// System.out.println(result.forestTotal);
		// System.out.println(result.goodsTotal);
		// System.out.println(result.governanceTotal);
		// System.out.println(result.grazTotal);
		// System.out.println(result.mobilityTotal);
		// System.out.println(result.servicesTotal);
		// System.out.println(result.shelterTotal);
		// //1.10.10 - vegan but all value coef = 2
		// for(int i = 0; i < answers.length; i++)
		// if(answers[i] != -1)
		// System.out.println(questions[i].mQuestion);

	}
}
